﻿  using Maticsoft.BLL;
    using Maticsoft.Model;

    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Utils;

    public partial class WebPage_Project_BianjiXiangmu : TeamBBasePage
    {
        protected string begin = "";
        protected string beginCss;
       
        private Maticsoft.BLL.Coupon couponbll = new Maticsoft.BLL.Coupon();
       
        protected string end = "";
        protected string endCss;
    
        protected int headSum = 0;
        protected string headYes = "";
       
        protected bool hidden = false;
        
        protected string kuaidi = "";
        protected string kuaidi1 = "";
        private Maticsoft.BLL.Team teams = new Maticsoft.BLL.Team();
        private ListItem teamType = null;
        protected string youhuiquan = "";
        protected string youhuiquan1 = "";

        private void getContent()
        {
            ListItem item2;
            Maticsoft.Model.Team model = this.teams.GetModel(Convert.ToInt32(base.Request.QueryString["id"].ToString()));
            if (model != null)
            {
                this.Literal1.Text = model.Title;
                if (model.Manualupdate == 1)
                {
                    this.headYes = "checked=\"checked\"";
                    this.headSum = model.Now_number;
                }
                else
                {
                    this.headYes = "";
                    this.headSum = model.Now_number;
                }
            }
            this.teamType = new ListItem();
            this.teamType.Text = "团购项目";
            this.teamType.Value = "normal";
            if (model.Team_type == "normal")
            {
                this.teamType.Selected = true;
            }
            this.projectType.Items.Add(this.teamType);
            this.teamType = new ListItem();
            this.teamType.Text = "秒杀项目";
            this.teamType.Value = "seconds";
            if (model.Team_type == "seconds")
            {
                this.teamType.Selected = true;
            }
            this.projectType.Items.Add(this.teamType);
            this.teamType = new ListItem();
            this.teamType.Text = "热销商品";
            this.teamType.Value = "goods";
            if (model.Team_type == "goods")
            {
                this.hidden = true;
                this.teamType.Selected = true;
            }
            this.projectType.Items.Add(this.teamType);
            this.projectType.DataBind();
            Maticsoft.BLL.Category category = new Maticsoft.BLL.Category();
            ListItem item = new ListItem();
            item.Text = "全部城市";
            item.Value = "0";
            this.city.Items.Add(item);
            foreach (Maticsoft.Model.Category category2 in category.GetModelList("Zone='city'"))
            {
                item2 = new ListItem();
                item2.Text = category2.Name;
                item2.Value = category2.Id.ToString();
                if ((model != null) && (category2.Id == model.City_id))
                {
                    item.Selected = true;
                }
                this.city.Items.Add(item2);
            }
            this.city.DataBind();
            ListItem item3 = new ListItem();
            item3.Text = "====请选择分类=====";
            item3.Value = "-1";
            this.groupType.Items.Add(item3);
            foreach (Maticsoft.Model.Category category2 in category.GetModelList("Zone='group'"))
            {
                item2 = new ListItem();
                item2.Text = category2.Name;
                item2.Value = category2.Id.ToString();
                if (category2.Id == model.Group_id)
                {
                    item2.Selected = true;
                }
                this.groupType.Items.Add(item2);
            }
            this.groupType.DataBind();
            Maticsoft.BLL.Partner partner = new Maticsoft.BLL.Partner();
            ListItem item4 = new ListItem();
            item4.Text = "===========请选择商户============";
            item4.Value = "-1";
            this.shanghu.Items.Add(item4);
            foreach (Maticsoft.Model.Partner partner2 in partner.GetModelList(""))
            {
                item2 = new ListItem();
                item2.Text = partner2.Username;
                item2.Value = partner2.Id.ToString();
                if ((model != null) && (partner2.Id == model.Partner_id))
                {
                    item2.Selected = true;
                }
                this.shanghu.Items.Add(item2);
            }
            ListItem item5 = new ListItem();
            item5.Text = "以购买成功人数成团";
            item5.Value = "Y";
            ListItem item6 = new ListItem();
            item6.Text = "以产品购买数量成团";
            item6.Value = "N";
            if (model.Conduser == "Y")
            {
                item5.Selected = true;
            }
            else
            {
                item6.Selected = true;
            }
            this.Conduse.Items.Add(item5);
            this.Conduse.Items.Add(item6);
            this.Conduse.DataBind();
            ListItem item7 = new ListItem();
            item7.Text = "仅购买一次";
            item7.Value = "Y";
            ListItem item8 = new ListItem();
            item8.Text = "可购买多次";
            item8.Value = "N";
            if (model.Buyonce == "Y")
            {
                item7.Selected = true;
            }
            else
            {
                item8.Selected = true;
            }
            this.Buyonce.Items.Add(item7);
            this.Buyonce.Items.Add(item8);
            this.Buyonce.DataBind();
            this.shanghu.DataBind();
            this.HiddenField1.Value = base.Request.QueryString["id"].ToString();
            if (model != null)
            {
                this.title.Value = model.Title;
                this.market_price.Value = model.Market_price.ToString();
                this.teamPrice.Value = model.Team_price.ToString();
                this.minnumber.Value = model.Min_number.ToString();
                this.maxnumber.Value = model.Max_number.ToString();
                this.pernumber.Value = model.Per_number.ToString();
                if (model.Team_type == "seconds")
                {
                    this.begintime.Value = model.Begin_time.ToString("yyyy-MM-dd HH:mm:ss");
                    this.beginCss = "datetime";
                    this.endtime.Value = model.End_time.ToString("yyyy-MM-dd HH:mm:ss");
                    this.endCss = "datetime";
                }
                else
                {
                    this.begintime.Value = model.Begin_time.ToString("yyyy-MM-dd");
                    this.beginCss = "date";
                    this.endtime.Value = model.End_time.ToString("yyyy-MM-dd");
                    this.endCss = "date";
                }
                this.begin = model.Begin_time.ToString(" HH:mm:ss");
                this.end = model.End_time.ToString(" HH:mm:ss");
                this.expiretime.Value = model.Expire_time.ToString("yyyy-MM-dd");
                this.createsummary.Value = model.Summary.ToString();
                this.creatnotice.Value = model.Notice;
                this.createsort_order.Value = model.Sort_order.ToString();
                this.createcard.Value = model.Card.ToString();
                this.createbonus.Value = model.Bonus.ToString();
                this.createproduct.Value = model.Product;
                this.ImageSet.InnerText = model.Image;
                if (model.Image1 != "")
                {
                    this.hfimg1.Value = model.Image1;
                    this.lblImg1.InnerHtml = string.Concat(new object[] { model.Image1, "<span>&nbsp;&nbsp;</span><a href='Project_BianjiXiangmu.aspx?del=", model.Id, "&image=1'>删除</a>" });
                }
                if (model.Image2 != "")
                {
                    this.lblImg2.InnerHtml = string.Concat(new object[] { model.Image2, "<span>&nbsp;&nbsp;</span><a href=' Project_BianjiXiangmu.aspx?del=", model.Id, "&image=2'>删除</a>" });
                }
                else
                {
                    this.lblImg2.InnerHtml = "";
                }
                this.createflv.Value = model.Flv;
                this.createdetail.Value = model.Detail;
                this.createuserreview.Value = model.Userreview;
                this.creatsystemreview.Value = model.Systemreview;
                this.createcredit.Value = model.Credit.ToString();
                this.createmobile.Value = model.Mobile;
                this.createaddress.Value = model.Address;
                this.createfare.Value = model.Fare.ToString();
                this.createfarefree.Value = model.Farefree.ToString();
                this.createexpress.Value = model.Express;
                this.city.SelectedValue = model.City_id.ToString();
                this.groupType.SelectedValue = model.Group_id.ToString();
                this.Reach_time.Value = model.Reach_time.ToString();
            }
            ListItem item9 = new ListItem();
            item9.Text = "优惠券";
            item9.Value = "coupon";
            ListItem item10 = new ListItem();
            item10.Text = "快递";
            item10.Value = "express";
            if ((model != null) && (model.Delivery == "coupon"))
            {
                this.youhuiquan1 = "checked=\"checked\"";
                this.kuaidi1 = "";
                this.youhuiquan = " display:block;";
                this.kuaidi = " display:none;";
            }
            else
            {
                this.kuaidi1 = "checked=\"checked\"";
                this.youhuiquan1 = "";
                this.kuaidi = " display:block;";
                this.youhuiquan = " display:none;";
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.HeaderControl1.Select = "program";
            if (!base.IsPostBack)
            {
                if (base.Request.QueryString["id"] != null)
                {
                    this.getContent();
                }
                if ((base.Request.QueryString["del"] != null) && (base.Request.QueryString["image"] != null))
                {
                    Maticsoft.Model.Team model = this.teams.GetModel(int.Parse(base.Request.QueryString["del"].ToString()));
                    int index = int.Parse(base.Request.QueryString["image"].ToString());
                    if (model != null)
                    {
                        this.teams.UpdateImg(model.Id, index);
                        base.SetSuccess("删除成功!");
                        base.Response.Redirect("Project_BianjiXiangmu.aspx?id=" + model.Id);
                    }
                }
            }
            if (base.Request.HttpMethod == "POST")
            {
                if (base.GetCookie("admin") != string.Empty)
                {
                    this.updateContent();
                }
                else
                {
                    this.SetError("您没有登录，请登录");
                    base.Response.Redirect("~/account/Login.aspx", true);
                }
            }
        }

        private void updateContent()
        {
            string str2;
            string fileName;
            string str4;
            int num;
            string str5;
            Exception exception;
            int count;
            Upload upload = new Upload();
            string path = "";
            Maticsoft.Model.Team model = this.teams.GetModel(Convert.ToInt32(this.HiddenField1.Value));
            model.Id = int.Parse(this.HiddenField1.Value);
            model.User_id = Convert.ToInt32(base.GetCookie("admin").ToString());
            if (this.projectType.SelectedItem.Value != "")
            {
                model.Team_type = this.projectType.SelectedItem.Value;
            }
            if (int.Parse(this.city.SelectedItem.Value) > -1)
            {
                model.City_id = int.Parse(this.city.SelectedItem.Value);
            }
            if (int.Parse(this.groupType.SelectedItem.Value) > -1)
            {
                model.Group_id = int.Parse(this.groupType.SelectedItem.Value);
            }
            model.Conduser = this.Conduse.SelectedItem.Value;
            model.Buyonce = this.Buyonce.SelectedItem.Value;
            model.Title = this.title.Value;
            model.Market_price = Convert.ToDecimal(this.market_price.Value);
            model.Team_price = Convert.ToDecimal(this.teamPrice.Value);
            model.Min_number = int.Parse(this.minnumber.Value);
            model.Max_number = int.Parse(this.maxnumber.Value);
            model.Per_number = int.Parse(this.pernumber.Value);
            DateTime dateTime = Helper.GetDateTime(this.begintime.Value, DateTime.MinValue);
            DateTime time2 = Helper.GetDateTime(this.endtime.Value, DateTime.MinValue);
            if ((dateTime != DateTime.MinValue) && (time2 != DateTime.MinValue))
            {
                TimeSpan span = (TimeSpan)(time2 - dateTime);
                if (this.expiretime.Value != "")
                {
                    model.Expire_time = Convert.ToDateTime(this.expiretime.Value.ToString() + " 23:59:59");
                    this.couponbll.updatetime(model.Id, model.Expire_time);
                }
                else
                {
                    model.Expire_time = Convert.ToDateTime(this.endtime.Value);
                    this.couponbll.updatetime(model.Id, Convert.ToDateTime(this.endtime.Value));
                }
            }
            model.System = "N";
            model.Begin_time = Convert.ToDateTime(this.begintime.Value);
            model.End_time = Convert.ToDateTime(this.endtime.Value.ToString());
            if (this.expiretime.Value != "")
            {
                model.Expire_time = Convert.ToDateTime(this.expiretime.Value + " 23:59:59");
            }
            else
            {
                model.Expire_time = Convert.ToDateTime(string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month, "-", DateTime.Now.Day + 1, " 23:59:59" }));
            }
            model.Summary = this.createsummary.Value;
            model.Notice = this.creatnotice.Value;
            model.Sort_order = int.Parse(this.createsort_order.Value);
            if (int.Parse(this.shanghu.SelectedItem.Value) > -1)
            {
                model.Partner_id = int.Parse(this.shanghu.SelectedItem.Value);
            }
            model.Card = int.Parse(this.createcard.Value);
            model.Bonus = int.Parse(this.createbonus.Value);
            model.Product = this.createproduct.Value;
            if (((this.Image.FileName != null) || (this.Image1.FileName != null)) || (this.Image2.FileName != null))
            {
                path = upload.setUpload();
            }
            if ((this.Image.FileName != null) && (this.Image.FileName.ToString() != ""))
            {
                str2 = "";
                if (this.Image.PostedFile.ContentLength > 0x7d000)
                {
                    this.SetError("请上传 512KB 以内的个人肖像图片!");
                    return;
                }
                fileName = this.Image.FileName;
                str4 = "";
                num = fileName.LastIndexOf(".");
                str5 = fileName.Substring(num);
                str4 = DateTime.Now.Ticks.ToString() + str5;
                try
                {
                    if (fileName != "")
                    {
                        str2 = base.Server.MapPath(path);
                        this.Image.PostedFile.SaveAs(str2 + str4);
                    }
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    base.Response.Write(exception);
                }
                path = path.Replace("~", "");
                model.Image = path + str4;
            }
            else
            {
                model.Image = this.ImageSet.InnerText;
            }
            if ((this.Image1.FileName != null) && (this.Image1.FileName.ToString() != ""))
            {
                if (this.Image1.PostedFile.ContentLength > 0x7d000)
                {
                    this.SetError("请上传 512KB 以内的个人肖像图片!");
                    return;
                }
                fileName = this.Image1.FileName;
                str4 = "";
                num = fileName.LastIndexOf(".");
                str5 = fileName.Substring(num);
                str4 = DateTime.Now.Ticks.ToString() + 1 + str5;
                try
                {
                    if (fileName != "")
                    {
                        str2 = base.Server.MapPath(path);
                        this.Image1.PostedFile.SaveAs(str2 + str4);
                    }
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    base.Response.Write(exception);
                }
                path = path.Replace("~", "");
                model.Image1 = path + str4;
            }
            else
            {
                model.Image1 = this.hfimg1.Value;
            }
            if ((this.Image2.FileName != null) && (this.Image2.FileName.ToString() != ""))
            {
                if (this.Image2.PostedFile.ContentLength > 0x7d000)
                {
                    this.SetError("请上传 512KB 以内的个人肖像图片!");
                    return;
                }
                fileName = this.Image2.FileName;
                str4 = "";
                num = fileName.LastIndexOf(".");
                str5 = fileName.Substring(num);
                str4 = DateTime.Now.Ticks.ToString() + 2 + str5;
                try
                {
                    if (fileName != "")
                    {
                        str2 = base.Server.MapPath(path);
                        this.Image2.PostedFile.SaveAs(str2 + str4);
                    }
                }
                catch (Exception exception3)
                {
                    exception = exception3;
                    base.Response.Write(exception);
                }
                path = path.Replace("~", "");
                model.Image2 = path + str4;
            }
            else
            {
                model.Image2 = this.hfimg2.Value;
            }
            if ((this.Reach_time.Value != null) && (this.Reach_time.Value != ""))
            {
                model.Reach_time = new DateTime?(Convert.ToDateTime(this.Reach_time.Value));
            }
            model.Flv = this.createflv.Value;
            model.Detail = this.createdetail.Value;
            model.Userreview = this.createuserreview.Value;
            model.Systemreview = this.creatsystemreview.Value;
            model.Delivery = base.Request.Form["radio"].ToString();
            if (model.Delivery == "coupon")
            {
                model.Fare = 0;
            }
            else
            {
                try
                {
                    model.Fare = Convert.ToInt32(this.createfare.Value);
                }
                catch (Exception)
                {
                    this.SetError("您输入的快递费用格式不正确,请重新输入");
                }
            }
            try
            {
                model.Credit = Convert.ToInt32(this.createcredit.Value);
            }
            catch (Exception)
            {
                this.SetError("您输入的消费返利格式不正确,请重新输入");
            }
            model.Mobile = this.createmobile.Value;
            model.Address = this.createaddress.Value;
            try
            {
                model.Farefree = Convert.ToInt32(this.createfarefree.Value);
            }
            catch (Exception)
            {
                this.SetError("您输入的名单数量不正确,请重新输入");
            }
            model.Express = this.createexpress.Value;
            if ((base.Request.Form["manualupdate"] != null) && (base.Request.Form["manualupdate"].ToString() != ""))
            {
                model.Manualupdate = 1;
                model.Now_number = Convert.ToInt32(base.Request.Form["now_number"].ToString());
                count = 0;
                if ((base.Request.Form["now_number"] != null) && (base.Request.Form["now_number"].ToString() != ""))
                {
                    count = Convert.ToInt32(base.Request.Form["now_number"].ToString());
                }
                else
                {
                    count = 0;
                }
                if (DateTime.Now > Convert.ToDateTime(this.endtime.Value))
                {
                    if ((count > model.Min_number) && (model.State != "soldout"))
                    {
                        model.State = "success";
                    }
                    else
                    {
                        model.State = "failure";
                    }
                }
                else if ((count > model.Max_number) && (model.Max_number != 0))
                {
                    model.State = "soldout";
                }
                else
                {
                    model.State = "none";
                }
            }
            else
            {
                model.Manualupdate = 0;
                Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
                count = order.GetModelList("State='pay' and Team_id=" + model.Id).Count;
                model.Now_number = count;
                if (model.Conduser == "Y")
                {
                    if (DateTime.Now > Convert.ToDateTime(this.endtime.Value))
                    {
                        if ((count > model.Min_number) && (model.State != "soldout"))
                        {
                            model.State = "success";
                        }
                        else
                        {
                            model.State = "failure";
                        }
                    }
                    else if ((count > model.Max_number) && (model.Max_number != 0))
                    {
                        model.State = "soldout";
                    }
                    else
                    {
                        model.State = "none";
                    }
                }
                else
                {
                    int num3 = 0;
                    foreach (Maticsoft.Model.Order order2 in order.GetModelList("State='pay' and Team_id=" + model.Id))
                    {
                        num3 += order2.Quantity;
                    }
                    model.Now_number = num3;
                    if (DateTime.Now > Convert.ToDateTime(this.endtime.Value))
                    {
                        if ((num3 > model.Min_number) && (model.State != "soldout"))
                        {
                            model.State = "success";
                        }
                        else
                        {
                            model.State = "failure";
                        }
                    }
                    else if ((num3 > model.Max_number) && (model.Max_number != 0))
                    {
                        model.State = "soldout";
                    }
                    else
                    {
                        model.State = "none";
                    }
                }
            }
            this.teams.Update(model);
            base.SetSuccess("更新成功");
            base.Response.Redirect("Project_BianjiXiangmu.aspx?id=" + model.Id);
        }
    }


